package com.iplant.mes.serviceImpl;

import com.iplant.aps.service.apsenum.aps.APSFirstCheckStatus;
import com.iplant.aps.service.po.oms.OMSOrder;
import com.iplant.aps.serviceImpl.dao.oms.OMSOrderDAO;
import com.iplant.base.po.OutResult;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.mes.service.BCMService;
import com.iplant.mes.service.po.bcm.BCMRule;
import com.iplant.mes.service.po.bcm.BCMRuleCode;
import com.iplant.mes.service.po.sfc.SFCTaskSelf;
import com.iplant.mes.serviceImpl.dao.bcm.BCMRuleCodeDAO;
import com.iplant.mes.serviceImpl.dao.bcm.BCMRuleDAO;
import com.iplant.mes.serviceImpl.dao.sfc.SFCTaskSelfDAO;
import com.iplant.pdm.service.po.fpc.FPCProduct;
import com.iplant.pdm.serviceImpl.dao.fpc.FPCProductDAO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class BCMServiceImpl implements BCMService {


    //region 构造函数
    public BCMServiceImpl() {
    }
    //endregion

    //region 单实例

    private static BCMService Instance = null;

    public static BCMService getInstance() {
        if (Instance == null)
            Instance = new BCMServiceImpl();

        return Instance;
    }
    //endregion

    @Override
    public ServiceResult<BCMRule> BCM_GetRuleByType(BMSEmployee wLoginUser, int wType, int wProductType) {
        ServiceResult<BCMRule> wResult = new ServiceResult<>();
        OutResult<Integer> wErrorCode = new OutResult<>(0);
        wResult.Result = BCMRuleDAO.getInstance().BCM_GetRuleByType(wLoginUser, wType, wProductType, wErrorCode);
        wResult.AddFaultCode(wErrorCode);
        return wResult;
    }

    @Override
    public ServiceResult<BCMRuleCode> BCM_CreateRuleCodeByType(BMSEmployee wLoginUser, OMSOrder wOMSOrder, int wType,
                                                               int wProductType) {
        OutResult<Integer> wErrorCode = new OutResult<>(0);
        BCMRule wBCMRule = BCMRuleDAO.getInstance().BCM_GetRuleByType(wLoginUser, wType, wProductType, wErrorCode);
        if (wErrorCode.Result != 0) {
            ServiceResult<BCMRuleCode> wResult = new ServiceResult<>();
            wResult.AddFaultCode(wErrorCode);
            return wResult;
        }
        return this.BCM_CreateRuleCodeByRule(wLoginUser, wOMSOrder, wBCMRule, false);
    }


    @Override
    public ServiceResult<List<BCMRuleCode>> BCM_CreateRuleCodeByTask(BMSEmployee wLoginUser, SFCTaskSelf wSFCTaskSelf
            , int wNumber) {

        ServiceResult<List<BCMRuleCode>> wResult = new ServiceResult<>(new ArrayList<>());

        OutResult<Integer> wErrorCode = new OutResult<>(0);

//        int wTaskID = wSFCTaskSelf.ID;
//        wSFCTaskSelf = SFCTaskSelfDAO.getInstance().SelectPO(wLoginUser, wTaskID, "", wErrorCode);
//        if (wErrorCode.Result != 0) {
//            wResult.AddFaultCode(wErrorCode);
//            return wResult;
//        }
//        if (wSFCTaskSelf == null || wSFCTaskSelf.ID <= 0) {
//            wResult.FaultCode = "工单ID:" + wTaskID + "不存在!";
//            return wResult;
//        }
//        if (wSFCTaskSelf.FirstCheckStatus == APSFirstCheckStatus.Locked.getValue()
//                || wSFCTaskSelf.FirstCheckStatus == APSFirstCheckStatus.CheckBad.getValue()) {
//            wResult.FaultCode = StringUtils.Format("工单【{0}】，订单【{1}】，型号【{2}】首检状态为`{3}`，禁止生产!", wSFCTaskSelf.Code,
//                    wSFCTaskSelf.OrderNo, wSFCTaskSelf.ProductCode,
//                    APSFirstCheckStatus.getEnumType(wSFCTaskSelf.FirstCheckStatus).getLabel());
//            return wResult;
//        }

        FPCProduct wFPCProduct = FPCProductDAO.getInstance().SelectPO(wLoginUser, wSFCTaskSelf.ProductID, "",
                wErrorCode);
        if (wErrorCode.Result != 0) {
            wResult.AddFaultCode(wErrorCode);
            return wResult;
        }
        OMSOrder wOMSOrder = OMSOrderDAO.getInstance().SelectPO(wLoginUser, wSFCTaskSelf.OrderID, "", wErrorCode);
        if (wErrorCode.Result != 0) {
            wResult.AddFaultCode(wErrorCode);
            return wResult;
        }
        BCMRule wBCMRule = BCMRuleDAO.getInstance().BCM_GetRuleByType(wLoginUser, wSFCTaskSelf.ComponentType,
                wFPCProduct.ProductTypeID, wErrorCode);
        if (wErrorCode.Result != 0) {
            wResult.AddFaultCode(wErrorCode);
            return wResult;
        }
        return this.BCM_CreateRuleCodeByRule(wLoginUser, wOMSOrder, wBCMRule, wNumber, false);
    }

    @Override
    public ServiceResult<List<BCMRuleCode>> BCM_CreateRuleCodeByType(BMSEmployee wLoginUser, OMSOrder wOMSOrder,
                                                                     int wType, int wProductType, int wNumber) {
        OutResult<Integer> wErrorCode = new OutResult<>(0);
        BCMRule wBCMRule = BCMRuleDAO.getInstance().BCM_GetRuleByType(wLoginUser, wType, wProductType, wErrorCode);
        if (wErrorCode.Result != 0) {
            ServiceResult<List<BCMRuleCode>> wResult = new ServiceResult<>();
            wResult.AddFaultCode(wErrorCode);
            return wResult;
        }
        if (wBCMRule == null) {
            ServiceResult<List<BCMRuleCode>> wResult = new ServiceResult<>();
            wResult.FaultCode += "未设置可用规则！";
            return wResult;
        }
        return this.BCM_CreateRuleCodeByRule(wLoginUser, wOMSOrder, wBCMRule, wNumber, false);
    }

    @Override
    public ServiceResult<BCMRuleCode> BCM_CreateRuleCodeByRule(BMSEmployee wLoginUser, OMSOrder wOMSOrder,
                                                               BCMRule wBCMRule, boolean wIsSelect) {
        ServiceResult<BCMRuleCode> wResult = new ServiceResult<>();
        if (wBCMRule == null || wBCMRule.ID <= 0) {
            wResult.FaultCode = RetCode.SERVER_RST_ERROR_OUT;
            return wResult;
        }
        OutResult<Integer> wErrorCode = new OutResult<>(0);
        if (wIsSelect) {
            wBCMRule = BCMRuleDAO.getInstance().SelectPO(wLoginUser, wBCMRule.ID, "", BCMRule.class, wErrorCode);
            if (wErrorCode.Result != 0) {
                wResult.AddFaultCode(wErrorCode);
                return wResult;
            }

            if (wBCMRule == null || wBCMRule.ID <= 0) {
                wResult.FaultCode += "此条码规则不存在";
                return wResult;
            }
        }


        BCMRuleCode wBCMRuleCode = BCMRuleCodeDAO.getInstance().BCM_CreateBarCode(wLoginUser, wOMSOrder, wBCMRule,
                wErrorCode);
        if (wErrorCode.Result != 0) {
            wResult.AddFaultCode(wErrorCode);
            return wResult;
        }
        BCMRuleDAO.getInstance().UpdatePo(wLoginUser, wBCMRule, wErrorCode);
        if (wErrorCode.Result != 0) {
            wResult.AddFaultCode(wErrorCode);
            return wResult;
        }
        wResult.Result = wBCMRuleCode;
        return wResult;
    }

    @Override
    public ServiceResult<List<BCMRuleCode>> BCM_CreateRuleCodeByRule(BMSEmployee wLoginUser, OMSOrder wOMSOrder,
                                                                     BCMRule wBCMRule, int wNumber, boolean wIsSelect) {
        ServiceResult<List<BCMRuleCode>> wResult = new ServiceResult<>(new ArrayList<>());
        if (wBCMRule == null || wBCMRule.ID <= 0) {
            wResult.FaultCode = RetCode.SERVER_RST_ERROR_OUT;
            return wResult;
        }

        OutResult<Integer> wErrorCode = new OutResult<>(0);
        if (wIsSelect) {
            wBCMRule = BCMRuleDAO.getInstance().SelectPO(wLoginUser, wBCMRule.ID, "", BCMRule.class, wErrorCode);
            if (wErrorCode.Result != 0) {
                wResult.AddFaultCode(wErrorCode);
                return wResult;
            }

            if (wBCMRule == null || wBCMRule.ID <= 0) {
                wResult.FaultCode += "此条码规则不存在";
                return wResult;
            }
        }

        if (wNumber < 1)
            wNumber = 1;

        if (wNumber > 100)
            wNumber = 100;
        BCMRuleCode wBCMRuleCode;
        String wLastBarCode = "";
        while (wNumber > 0) {
            if (StringUtils.isEmpty(wLastBarCode))
                wBCMRuleCode = BCMRuleCodeDAO.getInstance().BCM_CreateBarCode(wLoginUser, wOMSOrder, wBCMRule,
                        wErrorCode);
            else
                wBCMRuleCode = BCMRuleCodeDAO.getInstance().BCM_CreateBarCode(wLoginUser, wBCMRule, wLastBarCode,
                        wErrorCode);
            if (wErrorCode.Result != 0) {
                wResult.AddFaultCode(wErrorCode);
                return wResult;
            }
            wResult.Result.add(wBCMRuleCode);
            wLastBarCode = wBCMRuleCode.Code;
            wNumber--;
        }


        BCMRuleDAO.getInstance().UpdatePo(wLoginUser, wBCMRule, wErrorCode);
        if (wErrorCode.Result != 0) {
            wResult.AddFaultCode(wErrorCode);
            return wResult;
        }
        return wResult;
    }


}
